﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Drawing.Imaging;

using LowLevelGraphics.Types.Interval;

namespace LowLevelGraphics.Filter
{
    /// <summary>
    /// Converts color images into pseudocolor images this is used for example for medical applications
    /// or biological images.
    /// </summary>
    public class Pseudofarben : BaseImageFilter
    {
        protected IntInterval m_R = null;
        protected IntInterval m_G = null;
        protected IntInterval m_B = null;

        protected int[] m_Rvalue = new int[256];
        protected int[] m_Gvalue = new int[256];
        protected int[] m_Bvalue = new int[256];

        /// <summary>
        /// default constructor
        /// </summary>
        public Pseudofarben()
            : this(new IntInterval(0, 255), new IntInterval(0, 255), new IntInterval(0, 255))
        {
            for (int i = 0; i <= 255; i++)
            {
                m_Rvalue[i] = i;
                m_Gvalue[i] = 255 - i;
                m_Bvalue[i] = (int)(Math.Sqrt(i) * 2.0f);
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="_r"></param>
        /// <param name="_g"></param>
        /// <param name="_b"></param>
        public Pseudofarben(IntInterval _r, IntInterval _g, IntInterval _b)
        {
            m_R = _r;
            m_G = _g;
            m_B = _b;
        }

        /// <summary>
        /// used by Cclone to create a deep copy
        /// </summary>
        /// <param name="_pseudoFarben"></param>
        internal Pseudofarben(Pseudofarben _pseudoFarben)
        {
        }

        /// <summary>
        /// executes this filter
        /// </summary>
        /// <param name="_bitmap"></param>
        /// <returns></returns>
        public override UnsafeBitmap Execute(UnsafeBitmap _bitmap)
        {
            int x = 0;
            int y = 0;
            int nWidth = _bitmap.Width;
            int nHeight = _bitmap.Height;

            Color color = Color.Transparent;
            for (y = 0; y < nHeight; y++)
            {
                for (x = 0; x < nWidth; x++)
                {
                    color = _bitmap.GetPixel(x, y);
                    _bitmap.SetPixel(x, y, Color.FromArgb(m_Rvalue[color.R], m_Gvalue[color.G], m_Bvalue[color.B]));
                }
            }
            return _bitmap;
        }

        /// <summary>
        /// used to create a deep copy
        /// </summary>
        /// <returns></returns>
        public override object Clone()
        {
            return new Pseudofarben(this);
        }
    }
}
